package com.yaoweigroup.community.ui.bm;

import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Common;
import com.yaoweigroup.community.config.Constant;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.UserInfo;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.LoginUtil;
import com.yaoweigroup.community.util.SharePreferenceUtil;
import com.yaoweigroup.community.util.UtilHttp;

/**
 * 评论
 * 
 * @author xi
 * 
 */
public class CommentEditActivity extends Activity implements OnClickListener {

	private RelativeLayout left_rl;
	private Button ok_btn;
	private EditText averge_et;
	private EditText comment_et;
	private RatingBar total_ratingbar;
	private RatingBar des_ratingbar;
	private RatingBar evi_ratingbar;
	private RatingBar service_ratingbar;

	private int total = 1;
	private int service = 3;
	private int des = 3;
	private int evi = 3;
	private UserInfo user;
	private SharePreferenceUtil util;
	private int id;
	private Dialog dialog;
	private ObjectMapper mapper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		setContentView(R.layout.activity_comment_edit);
		util = new SharePreferenceUtil(this, Constant.prf);
		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);
		mapper = new ObjectMapper();
		ok_btn = (Button) findViewById(R.id.ok_btn);
		ok_btn.setOnClickListener(this);
		averge_et = (EditText) findViewById(R.id.averge_et);
		comment_et = (EditText) findViewById(R.id.comment_et);
		total_ratingbar = (RatingBar) findViewById(R.id.total_ratingbar);
		des_ratingbar = (RatingBar) findViewById(R.id.des_ratingbar);
		evi_ratingbar = (RatingBar) findViewById(R.id.evi_ratingbar);
		service_ratingbar = (RatingBar) findViewById(R.id.service_ratingbar);

		total_ratingbar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						total = (int) rating;
					}
				});
		des_ratingbar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						des = (int) rating;

					}
				});
		evi_ratingbar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						evi = (int) rating;

					}
				});
		service_ratingbar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						service = (int) rating;

					}
				});
		id = getIntent().getIntExtra("id", 0);
		dialog = DialogUtil.createWaitDialog(this, "评论中..");
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		case R.id.ok_btn:
			doSubmit();
			break;
		}
	}

	private void doSubmit() {
		user = (UserInfo) util.loadObject("user");
		if (user == null) {
			Toast.makeText(this, "请登陆!", 500).show();
			return;
		}
		String content = comment_et.getText().toString();
		String avi = averge_et.getText().toString();
		if (content.length() == 0 || avi.length() == 0) {

			Toast.makeText(this, "评论内容 或 人均消费 不能为空", 500).show();
			return;
		} else if (avi.length() > 4) {
			Toast.makeText(this, "消费金额不能超过4位数", 500).show();
			return;
		}
		dialog.show();
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("Uid", user.getUid());
		jsonUtil.add("id", id);
		jsonUtil.add("content", content);
		jsonUtil.add("score", total);
		jsonUtil.add("FlavorNum", des);
		jsonUtil.add("EnvironmentNum", evi);
		jsonUtil.add("ServiceNum", service);
		jsonUtil.add("Perconsume", Integer.parseInt(avi));
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString(), "UTF-8");
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
		params.setContentType("application/json;charset=UTF-8");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.setCommodityComment,
				params, new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						String result = responseInfo.result;
						try {
							JSONObject obj = new JSONObject(result);
							int state = obj.getInt("state");
							if (state == 200) {
								handler.sendEmptyMessage(SUBMIT_SUCCESS);
							} else if (state == 10000) {
								handler.sendEmptyMessage(NEED_LOGIN);
							} else {
								handler.sendEmptyMessage(SUBMIT_FAIL);
							}
						} catch (Exception e) {
							handler.sendEmptyMessage(SUBMIT_FAIL);
							LogUtil.e("wxf", e.toString());
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(SUBMIT_FAIL);

					}

				});
	}

	public final int SUBMIT_SUCCESS = 0x2210;
	public final int SUBMIT_FAIL = SUBMIT_SUCCESS + 1;
	public final int NEED_LOGIN = SUBMIT_SUCCESS + 2;
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case SUBMIT_SUCCESS:
				dialog.dismiss();
				Toast.makeText(CommentEditActivity.this, "评论成功", 500).show();
				finish();
				break;
			case SUBMIT_FAIL:
				dialog.dismiss();
				Toast.makeText(CommentEditActivity.this, "评论失败", 500).show();

				break;
			case NEED_LOGIN:
				LoginUtil.isLoginSuccess(CommentEditActivity.this, handler,
						mapper);
				break;
			case LoginUtil.LOGIN_FAIL:
				dialog.dismiss();
				Toast.makeText(CommentEditActivity.this, "评论成功", 500).show();

				break;
			case LoginUtil.LOGIN_SUCCESS:
				doSubmit();
				break;
			}

		};
	};
}
